﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GameDesigner.Displayers;
using GameLibrary.Model;
using GameLibrary.Model.Play;
using GameLibrary.Model.Core;
using Microsoft.VisualBasic.PowerPacks;

namespace GameDesigner.Source
{
    public abstract class GameDisplayer
    {
        public delegate void ShapeClickedHandler(SimpleShape shape);
        public event ShapeClickedHandler ShapeClickedEvent;

        private readonly Dictionary<GameLocation, SimpleShape> _Shapes = 
            new Dictionary<GameLocation, SimpleShape>();
        private readonly List<ElementDisplayer> _Displayers = 
            new List<ElementDisplayer>();
        private readonly GameDef _GameDef;
        private readonly ShapeContainer _Container;

        public GameDisplayer(GameDef game, ShapeContainer container) 
        {
            _GameDef = game;
            _Container = container;
            game.GameElementDefs.ElementRemovedEvent += this.OnElementRemoved;
            Init();
        }

        private void Init()
        {
            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    CreateShape(new GameLocation(x, y));
                }
            }
        }

        private RectangleShape CreateShape(GameLocation loc)
        {
            RectangleShape shape = new RectangleShape();
            shape.BorderColor = Color.Black;
            shape.FillColor = Color.Black;
            shape.FillStyle = FillStyle.Transparent;
            shape.BackStyle = BackStyle.Opaque;
            shape.BackColor = Color.WhiteSmoke;

            shape.Size = new Size(49, 49);
            shape.Location = loc.ToPoint();
            _Container.Shapes.Add(shape);
            _Shapes.Add(loc, shape);

            shape.MouseMove += new MouseEventHandler(this.OnShapeMouseEvent);
            shape.MouseLeave += new EventHandler(this.OnShapeMouseLeaveEvent);
            shape.Click += new EventHandler(this.OnShapeClickEvent);

            return shape;
        }

        public void ClearShape(GameLocation loc)
        {
            SimpleShape shape = _Shapes[loc];
            shape.BorderWidth = 2;
            shape.FillStyle = FillStyle.Transparent;
            shape.FillGradientStyle = FillGradientStyle.None;

            shape.BackgroundImage = null;
        }

        public List<ElementDisplayer> Displayers
        {
            get { return _Displayers; }
        }

        public Dictionary<GameLocation, SimpleShape> Shapes
        {
            get { return _Shapes; }
        }

        public GameDef GameDef
        {
            get { return _GameDef; }
        }

        public ShapeContainer ShapeContainer 
        {
            get { return _Container; }
        }

        private void OnShapeMouseEvent(Object target, MouseEventArgs e)
        {
            SimpleShape s = (SimpleShape)target;
            s.BorderColor = Color.Red;
        }

        private void OnShapeClickEvent(Object target, EventArgs e)
        {
            SimpleShape s = (SimpleShape)target;
            if (ShapeClickedEvent != null)
            {
                ShapeClickedEvent(s);
            }
        }

        private void OnShapeMouseLeaveEvent(Object target, EventArgs e)
        {
            SimpleShape s = (SimpleShape)target;
            s.BorderColor = Color.Black;
        }

        private void OnElementRemoved(GameElementDef subject)
        {
            foreach (ElementDisplayer displayer in _Displayers)
            {
                if (displayer.Location == subject.Location)
                {
                    displayer.DisplayIcon();
                    break;
                }
            }
        }

    }
}
